package cn.iocoder.yudao.module.scenic.service.spotrelation;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.scenic.controller.admin.spotrelation.vo.SpotRelationPageReqVO;
import cn.iocoder.yudao.module.scenic.controller.admin.spotrelation.vo.SpotRelationSaveReqVO;
import cn.iocoder.yudao.module.scenic.dal.dataobject.spotrelation.SpotRelationDO;
import cn.iocoder.yudao.module.scenic.dal.mysql.spotrelation.SpotRelationMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scenic.enums.ErrorCodeConstants.SPOT_RELATION_NOT_EXISTS;

/**
 * 线路实践点关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SpotRelationServiceImpl implements SpotRelationService {

    @Resource
    private SpotRelationMapper spotRelationMapper;

    @Override
    public Long createSpotRelation(SpotRelationSaveReqVO createReqVO) {
        // 插入
        SpotRelationDO spotRelation = BeanUtils.toBean(createReqVO, SpotRelationDO.class);
        spotRelationMapper.insert(spotRelation);
        // 返回
        return spotRelation.getId();
    }

    @Override
    public void updateSpotRelation(SpotRelationSaveReqVO updateReqVO) {
        // 校验存在
        validateSpotRelationExists(updateReqVO.getId());
        // 更新
        SpotRelationDO updateObj = BeanUtils.toBean(updateReqVO, SpotRelationDO.class);
        spotRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpotRelation(Long id) {
        // 校验存在
        validateSpotRelationExists(id);
        // 删除
        spotRelationMapper.deleteById(id);
    }

    private void validateSpotRelationExists(Long id) {
        if (spotRelationMapper.selectById(id) == null) {
            throw exception(SPOT_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public SpotRelationDO getSpotRelation(Long id) {
        return spotRelationMapper.selectById(id);
    }

    @Override
    public PageResult<SpotRelationDO> getSpotRelationPage(SpotRelationPageReqVO pageReqVO) {
        return spotRelationMapper.selectPage(pageReqVO);
    }

}